/* 

source: [-4,1, 10, -1, -8, 3, 6, -9, 1]
result: [-4, -1,-8,-9,1,10,3,6,1]

*/

/* var exchange = function (nums) {
    let left = 0,
        right = nums.length - 1;
    while (left < right) {
        while (nums[left] % 2 !== 0 && left < right) left++;
        while (nums[right] % 2 === 0 && left < right) right--;
        if (left < right) {
            let temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
    }

    return nums;
};

console.log(exchange([1, 3, 5])) */

var exchange = function (nums) {
    let left = 0,
        right = nums.length - 1;
    while (left < right) {
        if (nums[left] % 2 !== 0) {
            left++;
            continue;
        };
        if (nums[right] % 2 === 0) {
            right--;
            continue;
        };
        
        let temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    return nums;
};

console.log(exchange([1, 3, 5]))